Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

The Example JSP Pages

This chapter illustrates JSTL with excerpts from the JSP version of the Duke's Bookstore application discussed in Chapter 12 rewritten to replace the JavaBeans component database helper object with direct calls to the database via the JSTL SQL tags. For most applications, it is better to encapsulate calls to a database in a bean. JSTL includes SQL tags for situations where a new application is being prototyped and the overhead of creating a bean may not be warranted.

The source for the Duke's Bookstore application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore4/ directory created when you unzip the tutorial bundle (see About the Examples). A sample bookstore4.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build, package, deploy, and run the example:

  1. Build and package the bookstore common files as described in Duke's Bookstore Examples.
  2. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/bookstore4/.
  3. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore4/build/ directory.
  4. Start the J2EE application server.
  5. Perform all the operations described in Accessing Databases from Web Applications.
  6. Start deploytool.
  7. Create a Web application called bookstore4 by running the New Web Application Wizard. Select FileRight ArrowNewRight ArrowWeb Application WAR.
  8. New Web Application Wizard
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. Click Browse and in the file chooser, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore4/.
    3. In the File Name field, enter bookstore4.
    4. Click Choose Module File.
    5. In the WAR Display Name field, enter bookstore4.
    6. In the Context Root field, enter /bookstore4.
    7. Click Edit to add the content files.
    8. In the Edit Contents dialog, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore4/build/. Select the JSP pages bookstore.jsp, bookdetails.jsp, bookcatalog.jsp, bookshowcart.jsp, bookcashier.jsp, bookreceipt.jsp and the database and template directories and click Add. Click OK.
    9. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and Click Add.
    10. Click OK.
    11. Click Next.
    12. Select the JSP radio button.
    13. Click Next.
    14. Select bookstore.jsp from the JSP Filename combo box.
    15. Click Finish.
  9. Add each of the Web components listed in Table 13-1. For each component:
    1. Select FileRight ArrowNewRight ArrowWeb Application WAR.
    2. Click the Add to Existing WAR Module radio button Since the WAR contains all of the JSP pages, you do not have to add any more content.
    3. Click Next.
    4. Select the JSP radio button and the Component Aliases checkbox.
    5. Click Next.
    6. Select the page from the JSP Filename combo box.
    7. Click Next.
    8. Click Add. Enter the alias.
    9. Click Finish.
      Table 13-1 Duke's Bookstore Web Components 
      Web Component Name
      JSP Page
      Component Alias
      bookstore
      bookstore.jsp
      /bookstore
      bookcatalog
      bookcatalog.jsp
      /bookcatalog
      bookdetails
      bookdetails.jsp
      /bookdetails
      bookshowcart
      bookshowcart.jsp
      /bookshowcart
      bookcashier
      bookcashier.jsp
      /bookcashier
      bookreceipt
      bookreceipt.jsp
      /bookreceipt
  10. Add the JSTL resource bundle basename context parameter.
    1. Select the Context tab.
    2. Click Add.
    3. Enter javax.servlet.jsp.jstl.fmt.localizationContext for the Coded Parameter.
    4. Enter messages.BookstoreMessages for the Value.
  11. Set prelude and codas for all JSP pages.
    1. Select the JSP Properties tab.
    2. Click the Add button next to the Name list.
    3. Enter bookstore4.
    4. Click the Add button next to the URL Pattern list.
    5. Enter /*.jsp.
    6. Click the Edit button next to the Include Preludes list.
    7. Click Add.
    8. Enter /template/prelude.jspf.
    9. Click OK.
    10. Click the Edit button next to the Include Codas list.
    11. Click Add.
    12. Enter /template/coda.jspf.
    13. Click OK.
  12. Add a resource reference for the database.
    1. Select the Resource Refs tab.
    2. Click Add.
    3. Enter jdbc/BookDB in the Coded Name field.
    4. Accept the default type javax.sql.DataSource.
    5. Accept the default authorization Container.
    6. Accept the default selected Shareable.
    7. Enter jdbc/BookDB in the JNDI name field of the Deployment setting for jdbc/BookDB frame.
  13. Select FileRight ArrowSave.
  14. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. Click OK.
    3. A popup dialog will display the results of the deployment. Click Close.
  15. Open the bookstore URL http://localhost:1024/bookstore4/bookstore.

See Troubleshooting for help with diagnosing common problems.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.